package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.RecommendVideo;
import com.itheima.domain.mongo.Video;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.VideoService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 小视频列表
     * @param page
     * @param pageSize
     * @param loginUserId
     * @return
     */
    @Override
    public PageBeanVo findSmallVideos(Integer page, Integer pageSize, Long loginUserId) {
        int startIndex = (page-1)*pageSize;
        Query query = new Query(
                Criteria.where("userId").is(loginUserId)
        ).with(Sort.by(Sort.Order.desc("date")))
                .skip(startIndex).limit( pageSize );
        //1.查询推荐信息
        List<RecommendVideo> recommendVideoList = mongoTemplate.find(query, RecommendVideo.class);

        //如果没有数据 模拟假数据
        //if(){}

        //2.转换成video数据
        List<Video> videoList = new ArrayList<>();
        for (RecommendVideo recommendVideo : recommendVideoList) {
            /*Query query2 = new Query(
                    Criteria.where("videoId").is(recommendVideo.getVideoId())
            );*/
            //只要有一个推荐 应该有一个视频
            //Video video = mongoTemplate.findOne(query2 , Video.class);

            Video video = mongoTemplate.findById(recommendVideo.getVideoId(), Video.class);
            //查询视频信息
            videoList.add(video);
        }

        long count = mongoTemplate.count(query, RecommendVideo.class);
        return new PageBeanVo(page, pageSize , count , videoList);
    }

    @Autowired
    private IdService idService;
    /**
     * 保存视频对象
     * @param video
     */
    @Override
    public void save(Video video) {
        video.setVid(idService.getNextId(ConstantUtil.VIDEO_ID)); //大数据推荐的id
        video.setCreated(System.currentTimeMillis()); //时间戳
        mongoTemplate.save(video);
    }

    /**
     * 根据用户id查询用户的视频数据
     * @param page
     * @param pagesize
     * @param uid
     * @return
     */
    @Override
    public PageBeanVo findVideoByUid(Integer page, Integer pagesize, Long uid) {
        int startIndex = (page-1)*pagesize;
        //根据用户id查询用户的视频数据
        Query query = new Query(
                Criteria.where("userId").is(uid)
        ).with(Sort.by(Sort.Order.desc("created")))
                .skip(startIndex).limit(pagesize);
        List<Video> videoList = mongoTemplate.find(query, Video.class);
        long count = mongoTemplate.count(query, Video.class);
        return new PageBeanVo(page, pagesize , count, videoList);
    }

    //根据id查询视频
    @Override
    public Video findVideoByVideoId(String videoId) {

        return mongoTemplate.findById(videoId,Video.class);
    }
}
